use utils::trie_tree::ScanResult;

mod pinyin {
    include!(concat!(env!("OUT_DIR"), "/pinyin.rs"));
}

mod pitch {
    include!(concat!(env!("OUT_DIR"), "/pitch.rs"));
}

mod wadegiles {
    include!(concat!(env!("OUT_DIR"), "/wadegiles.rs"));
}

pub fn converter(input: &str) -> String {
    // 将输入的汉字转换为拼音
    let pinyin = pinyin::SIGLETON
        .iter_scan(input.chars().collect::<Vec<_>>())
        .filter_map(|r| match r {
            ScanResult::Found(&v) => Some(v),
            ScanResult::NotFound(_) => None,
        })
        .collect::<Vec<_>>()
        .join(" ");
    // 将拼音转换为无音调的拼音
    let pinyin_chars = pinyin.chars().collect::<Vec<_>>();
    let non_pitch = pitch::SIGLETON
        .iter_scan(&pinyin_chars)
        .filter_map(|r| match r {
            ScanResult::Found(&v) => Some(v.to_string()),
            ScanResult::NotFound(idx) => pinyin_chars.get(idx).map(|c| c.to_string()),
        })
        .collect::<String>();
    // 将无音调的拼音转换为 Wade-Giles 拼音
    let non_pitch_chars = non_pitch.chars().collect::<Vec<_>>();
    let wadegiles_pinyin = wadegiles::SIGLETON
        .iter_scan(&non_pitch_chars)
        .filter_map(|r| match r {
            ScanResult::Found(&v) => Some(v.to_string()),
            ScanResult::NotFound(idx) => non_pitch_chars.get(idx).map(|c| c.to_string()),
        })
        .collect::<String>();
    // 首字母大写
    wadegiles_pinyin
        .chars()
        .enumerate()
        .map(|(idx, chr)| {
            if idx == 0 {
                chr.to_uppercase().next().unwrap()
            } else {
                chr
            }
        })
        .collect::<String>()
}
